package cn.kgc.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;

/**
 * 后端返回给前端的数据
 * @author wangyue
 * @version 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result <T>{
    /**
     * 接口返回的编码
     */
    private int code;

    /**
     * 返回的数据
     */
    private T data;

    /**
     * 返回的消息
     */
    private String msg;

    /**
     * 将服务端返回的数据封装到Result对象
     * @param data 服务端返回的数据
     */
    public static Result<Object> success(Object data){
        return Result.builder()
                .code(ResultConstant.SUCCESS.getCode())
                .data(data)
                .msg(ResultConstant.SUCCESS.getMsg()).build();
    }

    /**
     * 将服务端BaseException异常信息封装成Result对象
     */
    public static Result<Object> error(BaseException baseException){
        return Result.builder()
                .code(baseException.getCode())
                .msg(baseException.getMessage()).build();
    }

    /**
     * 将服务端错误码和错误信息封Result对象
     */
    public static Result<Object> error(int code,String msg){
        return Result.builder().code(code).msg(msg).build();
    }

    public static Result<Object> error(HashMap<String,String> map){
        return Result.builder()
                .code(ResultConstant.VALIDATE_ERROR.getCode())
                .data(map)
                .msg(ResultConstant.VALIDATE_ERROR.getMsg()).build();
    }
}